﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Common;

namespace EHR18
{
    public partial class FormMain : FrmBase
    {
       I_IO_Card io = IOFactory.getIO_Instance(GenneralConfig.instance.IO_TYPE);
        public FormMain()
        {
            InitializeComponent();
            var Frm = FormDisplay.getInstance();
            ShowMdiChildren(Frm);
           io.TrigEvent += io_TrigEvent;
        }

        void io_TrigEvent(ushort obj)
        {
            if ((obj |= 1 << 13)>0)
            {
                //   io.WriteBit(2, false);
                CountManager.Clear();
                ProductMachine.inst.ClearProducts();
                FormDisplay.getInstance().UpdateTotalStatistics();
            }
        }


        private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

            var btn = e.ClickedItem as ToolStripButton;
            if (btn != null)
            {

                if (btn.Text == "PLC信息")
                {
                    var Frm = UserData.GetInstance();
                    ShowMdiChildren(Frm);
                }
                else if (btn.Text == "实时统计")
                {
                    var Frm = FormDisplay.getInstance();
                    ShowMdiChildren(Frm);
                }
                else if (btn.Text == "数据查询")
                {
                    var Frm = FrmReport.getInstance();
                    ShowMdiChildren(Frm);

                }
                else if (btn.Text == "分项良率")
                {
                    var Frm = kanban2.getInstance();
                    ShowMdiChildren(Frm);
                }
            }
        }

        private void 串口设定ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void 退出EToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();

        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {

        }

        private void toolStripButton5_Click(object sender, EventArgs e)
        {

        }

        private void FormMain_FormClosed(object sender, FormClosedEventArgs e)
        {
            Environment.Exit(0);
        }

        private void pLC串口设定ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var frm = new FrmSerialPortSetting(PLCDevice.instance);
            frm.ShowDialog();
        }

        private void 电感1串口设定ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var frm = new FrmSerialPortSetting(LeakageDevice1.instance);
            frm.ShowDialog();
        }

        private void 漏感2串口设定ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var frm = new FrmSerialPortSetting(LeakageDevice2.instance);
            frm.ShowDialog();
        }

        private void 参数设定ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FrmModulesConfig frm = new FrmModulesConfig();
            frm.ShowDialog();
        }

        private void FormMain_Load(object sender, EventArgs e)
        {

        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {

        }

        private void toolStripMenuItem4_Click(object sender, EventArgs e)
        {
           Form frm=   FrmIOTrace.GetInstance();
            frm.StartPosition = FormStartPosition.CenterScreen;
            frm.FormBorderStyle = FormBorderStyle.FixedDialog;
            frm.Show();
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {

        }

        private void leakage4ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var frm = new FrmSerialPortSetting(LeakageDevice4.instance);
            frm.ShowDialog();
        }

        private void leakage3ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var frm = new FrmSerialPortSetting(LeakageDevice3.instance);
            frm.ShowDialog();
        }

        private void 串口设置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var frm = new FromSerialConfig();
            frm.StartPosition = FormStartPosition.CenterScreen;
            frm.Show();
        }

        private void 清空统计CToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ClearProductCount();
        }

        private static void ClearProductCount()
        {
            if (ProductMachine.inst.Products.Count > 0)
            {
                if (MessageBox.Show("请求清空计数,转盘中无产品?", "请确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
                {
                    return;
                }
            }
            CountManager.Clear();
            ProductMachine.inst.ClearProducts();
            FormDisplay.getInstance().UpdateTotalStatistics();
        }
    }
}
